home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / glimpse-2.1 / index / printx.c < prev    next >
C/C++ Source or Header  |  1995-05-16  |  7KB  |  263 lines

  1. /* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal.  All Rights Reserved. */
  2. /* ./glimpse/index/printx.c */
  3. /* Just to read the index by saying "printx < .glimpse_index | more" */
  4. /*
  5.  * To compile it just say "cc -o -I../agrep printx.c" and then say
  6.  * "cc -o printx printx.c io.o provided io.c has already been made.
  7.  */
  8. #include <stdio.h>
  9. #include "glimpse.h"
  10.  
  11. /*
  12.  * This stuff help us use io.c directly without copying stuff.
  13.  * get_set() had to be copied from glimpse since get_index.c
  14.  * has too much irrelevant stuff.
  15.  */
  16.  
  17. int memory_usage;
  18. char INDEX_DIR[MAX_LINE_LEN];
  19.  
  20. unsigned char dest_index_buf[REAL_INDEX_BUF];
  21. unsigned char src_index_buf[REAL_INDEX_BUF];
  22.  
  23. int num_filter;
  24. int filter_len[MAX_FILTER];
  25. CHAR *filter[MAX_FILTER];
  26. CHAR *filter_command[MAX_FILTER];
  27.  
  28. char *
  29. my_malloc(size)
  30. int    size;
  31. {
  32.     return malloc(size);
  33. }
  34.  
  35. int
  36. my_free(ptr)
  37. char    *ptr;
  38. {
  39.     return free(ptr);
  40. }
  41.  
  42. memagrep_search(len, pat, lenbuf, buf, lenout, out)
  43. {
  44.     return 0;
  45. }
  46.  
  47. /* Returns 1 if it is a Universal set, 0 otherwise. Constraint: WORD_END_MARK/ALL_INDEX_MARK must occur at or after buffer[0] */
  48. get_set(buffer, partfp, OneFilePerBlock, StructuredIndex, ByteLevelIndex)
  49.     unsigned char    *buffer;
  50.     FILE    *partfp;
  51.     int    OneFilePerBlock;
  52. {
  53.     int    bdx2, j;
  54.     int    ret;
  55.     int    x=0, y=0, diff, even_words=1, prevy;
  56.     int    indexattr = 0;
  57.     int    delim = encode8b(0);
  58.     int    curfreq = 0;
  59.  
  60.     /* buffer[0] is '\n', search must start from buffer[1] */
  61.     if (StructuredIndex) {
  62.         if (StructuredIndex < MaxNum8bPartition - 1) {
  63.             indexattr = decode8b(buffer[1]);
  64.             bdx2 = 2;
  65.         }
  66.         else if (StructuredIndex < MaxNum16bPartition - 1) {
  67.             indexattr = decode16b((buffer[1] << 8) | buffer[2]);
  68.             bdx2 = 3;
  69.         }
  70.         else {
  71.             indexattr = decode32b((buffer[1] << 24) | (buffer[2] << 16) | (buffer[3] << 8) | (buffer[4]));
  72.             bdx2 = 5;
  73.         }
  74.         printf("%d: ", indexattr);
  75.     }
  76.     else bdx2 = 1;
  77.  
  78.     if (OneFilePerBlock) while((bdx2<REAL_INDEX_BUF+1) && (buffer[bdx2] != WORD_END_MARK) && (buffer[bdx2] != ALL_INDEX_MARK)) {
  79.         if (isalnum(buffer[bdx2])) putchar(buffer[bdx2]);
  80.         bdx2 ++;
  81.     }
  82.     else while((bdx2<REAL_INDEX_BUF+1) && (buffer[bdx2] != WORD_END_MARK)) {
  83.         if (isalnum(buffer[bdx2])) putchar(buffer[bdx2]);
  84.         bdx2 ++;
  85.     }
  86.     if (bdx2 >= REAL_INDEX_BUF+1) {
  87.         printf("error\n");
  88.         return 0;
  89.     }
  90.  
  91.     printf(": ");
  92.     if (OneFilePerBlock && (buffer[bdx2] == ALL_INDEX_MARK)) {
  93.         printf("stop list\n");
  94.         return 1;
  95.     }
  96.     bdx2++;    /* bdx2 now points to the first byte of the offset */
  97.  
  98.     even_words = 1;
  99.     /* Code identical to that in merge_in() in glimpseindex */
  100.     if (OneFilePerBlock) {
  101.         get_block_numbers(&buffer[bdx2], &buffer[bdx2], partfp);
  102.         while((bdx2<REAL_INDEX_BUF) && (buffer[bdx2] != '\n') && (buffer[bdx2] != '\0')) {
  103.         /* First get the file name */
  104.         x = 0;
  105.         if (ByteLevelIndex) {
  106.             if (OneFilePerBlock <= MaxNum8bPartition) {
  107.             x = decode8b(buffer[bdx2]);
  108.             bdx2 ++;
  109.             }
  110.             else {
  111.             x = (buffer[bdx2] << 8) | buffer[bdx2+1];
  112.             x = decode16b(x);
  113.             bdx2 += 2;
  114.             }
  115.         }
  116.         else if (OneFilePerBlock <= MaxNum8bPartition) {
  117.             x = decode8b(buffer[bdx2]);
  118.             bdx2 ++;
  119.         }
  120.         else if (OneFilePerBlock <= MaxNum12bPartition) {
  121.             if (even_words) {
  122.             x = ((buffer[bdx2+1] & 0x0000000f) << 8) | buffer[bdx2];
  123.             x = decode12b(x);
  124.             bdx2 += 2;
  125.             even_words = 0;
  126.             }
  127.             else {    /* odd number of words so far */
  128.             x = ((buffer[bdx2-1] & 0x000000f0) << 4) | buffer[bdx2];
  129.             x = decode12b(x);
  130.             bdx2 ++;
  131.             even_words = 1;
  132.             }
  133.         }
  134.         else if (OneFilePerBlock <= MaxNum16bPartition) {
  135.             x = (buffer[bdx2] << 8) | buffer[bdx2+1];
  136.             x = decode16b(x);
  137.             bdx2 += 2;
  138.         }
  139.         printf("%d ", x);
  140.  
  141.         prevy = 0;
  142.         if (ByteLevelIndex) {
  143.             printf("[ ");
  144.             while ((bdx2<REAL_INDEX_BUF) && (buffer[bdx2] != '\n') && (buffer[bdx2] != '\0')) {
  145.             y = decode8b(buffer[bdx2]);
  146.             if ((y & 0x000000c0) == 0) {    /* one byte offset */
  147.                 diff = y&0x0000003f;
  148.                 y = prevy + diff;
  149.                 bdx2 ++;
  150.             }
  151.             else if ((y & 0x000000c0) == 0x40) {    /* two byte offset */
  152.                 diff = decode8b(buffer[bdx2+1]);
  153.                 y = prevy + (((y & 0x0000003f) * MaxNum8bPartition) + diff);
  154.                 bdx2 += 2;
  155.             }
  156.             else if ((y & 0x000000c0) == 0x80) {    /* three byte offset */
  157.                 diff = decode16b((buffer[bdx2+1] << 8) | buffer[bdx2+2]);
  158.                 y = prevy + (((y & 0x0000003f) * MaxNum16bPartition) + diff);
  159.                 bdx2 += 3;
  160.             }
  161.             else {    /* four byte offset */
  162.                 diff = decode24b((buffer[bdx2+1] << 16) | (buffer[bdx2+2] << 8) | buffer[bdx2+3]);
  163.                 y = prevy + (((y & 0x0000003f) * MaxNum24bPartition) + diff);
  164.                 bdx2 += 4;
  165.             }
  166.             prevy = y;
  167.             printf("%d ");
  168.             if ((bdx2<REAL_INDEX_BUF) && (buffer[bdx2] == delim)) {    /* look at offsets corr. to a new file now */
  169.                 bdx2 ++;
  170.                 break;
  171.             }
  172.             }
  173.             printf("] ");
  174.         }
  175.         }
  176.     }
  177.     else {
  178.         while((bdx2<MAX_INDEX_BUF) && (buffer[bdx2] != '\n') && (buffer[bdx2] != '\0') && (buffer[bdx2] < MAX_PARTITION)) {
  179.         printf("%d ", buffer[bdx2++]);
  180.         }
  181.     }
  182.     printf("\n");
  183.  
  184.     return 0;
  185. }
  186.  
  187. main(argc, argv)
  188. int    argc;
  189. char    *argv[];
  190. {
  191.     int c;
  192.     int in_word = 1;
  193.     char s[MAX_LINE_LEN];
  194.     FILE *i_in = stdin;
  195.     FILE *p_in;
  196.     int j, wordoffset, index, pat_size;
  197.     char indexnumberbuf[MAX_NAME_LEN];
  198.     int indexnumber, onefileperblock, structuredindex, bytelevelindex;
  199.  
  200.     if (argc <= 1) goto oldprintx;
  201.  
  202.     sprintf(s, "%s/%s", argv[1], ".glimpse_index");
  203.     if ((i_in = fopen(s, "r")) == NULL) {
  204.         fprintf(stderr, "cannot find %s\n", s);
  205.         exit(1);
  206.     }
  207.     sprintf(s, "%s/%s", argv[1], ".glimpse_partitions");
  208.     if ((p_in = fopen(s, "r")) == NULL) {
  209.         fprintf(stderr, "cannot find %s\n", s);
  210.         exit(1);
  211.     }
  212.  
  213.     /* modified the original in glimpse's main.c */
  214.     fgets(indexnumberbuf, 256, i_in);
  215.     if(strstr(indexnumberbuf, "1234567890"))
  216.         printf("indexed numbers\n");
  217.     else printf("not indexed numbers\n");
  218.     fscanf(i_in, "%%%d\n", &onefileperblock);
  219.     bytelevelindex = 0;
  220.     if (onefileperblock < 0) {
  221.         bytelevelindex = 1;
  222.         onefileperblock = -onefileperblock;
  223.         printf("byte level index\n");
  224.     }
  225.     else if (onefileperblock > 0) printf("file level index\n");
  226.     else printf("block level index\n");
  227.     fscanf(i_in, "%%%d\n", &structuredindex);
  228.     if (structuredindex <= 0) printf("attributes = 0\n");
  229.     else printf("attributes = %d\n", structuredindex);
  230.  
  231.     src_index_buf[0] = src_index_buf[REAL_INDEX_BUF - 1] = '\n';
  232.     while (fgets(&src_index_buf[1], REAL_INDEX_BUF-1, i_in)) {
  233.         get_set(src_index_buf, p_in, onefileperblock, structuredindex, bytelevelindex);
  234.         src_index_buf[0] = src_index_buf[REAL_INDEX_BUF - 1] = '\n';
  235.     }
  236.     fclose(i_in);
  237.     fclose(p_in);
  238.     exit(0);
  239.  
  240. oldprintx:
  241.     while((c = getc(i_in)) != EOF) {
  242.         if(in_word) {
  243.             if(c == 2) {
  244.                 in_word = 0;
  245.                 printf(": ");
  246.             }
  247.             else if (c==3) {
  248.                 in_word = 0;
  249.                 printf("= ");
  250.             }
  251.             else if (isalnum(c)) printf("%c", c);
  252.             else printf("<%d>", c);
  253.         }
  254.         else {
  255.             if(c == '\n') {
  256.                 in_word = 1;
  257.                 putchar('\n');
  258.             }
  259.             else printf(" %d", c);
  260.         }
  261.     }
  262. }
  263.